﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class DepartInfoService
    {
        DBHelper db = new DBHelper();
        BranchInfoService branchInfoService = new BranchInfoService();
        //查找所有DepartInfo信息
        public IList<DepartInfo> GetAllDepartInfo()
        {
            List<DepartInfo> departInfoList = new List<DepartInfo>();

            using (DataTable table = db.getDataTable("proc_getAllDepartInfo"))
            {
                foreach (DataRow row in table.Rows)
                {
                    DepartInfo departInfo = new DepartInfo();
                    departInfo.DepartId = (int)row["DepartId"];
                    departInfo.DepartName = (string)row["DepartName"];
                    departInfo.DepartPhone = (string)row["DepartPhone"];
                    departInfo.DepartEmail = (string)row["DepartEmail"];
                    int branchId = (int)row["BranchId"];
                    departInfo.Branch = branchInfoService.GetBranchInfoByBranchId(branchId);
                    departInfoList.Add(departInfo);
                }
                db.CloseConnection();
                return departInfoList;
            }
        }
        //根据departId查找DepartInfo信息
        public DepartInfo GetDepartInfoBuDepartInfoByDepartId(int departId)
        {
            using (SqlDataReader reader = db.getDataReader("proc_getDepartInfoByDepartId", new SqlParameter("@departId", departId)))
            {
                DepartInfo departInfo = new DepartInfo();
                if (reader.Read())
                {
                    departInfo.DepartId = (int)reader["DepartId"];
                    departInfo.DepartName = (string)reader["DepartName"];
                    departInfo.DepartPhone = (string)reader["DepartPhone"];
                    departInfo.DepartEmail = (string)reader["DepartEmail"];
                    int branchId = (int)reader["BranchId"];
                    departInfo.Branch = branchInfoService.GetBranchInfoByBranchId(branchId);
                  
                }
                reader.Close();
                db.CloseConnection();
                return departInfo;
            }

        }
        //根据BranchId查找departInfo 数量
        public int GetCountDepartInfoByBranchId(int branchId)
        {
            try
            {
                object a = db.getScalar("proc_getCountDepartInfoByBranchId", new SqlParameter("@BranchId", branchId));
                int result = Convert.ToInt32(db.getScalar("proc_getCountDepartInfoByBranchId", new SqlParameter("@BranchId", branchId)));
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //添加部门信息
        public int AddDepartInfo(DepartInfo departInfo)
        {

            try
            {
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@DepartName",departInfo.DepartName),
                    new SqlParameter("@DepartPhone",departInfo.DepartPhone),
                    new SqlParameter("@DepartEmail",departInfo.DepartEmail),
                    new SqlParameter("@BranchId",departInfo.Branch.BranchId)
                };
                int result = db.getCount("proc_insertDepartInfo", para);
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //根据DepartId修改部门信息
        public int UpdateDepartInfoByDepartId(DepartInfo departInfo)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@DepartId",departInfo.DepartId),
                    new SqlParameter("@DepartName",departInfo.DepartName),
                    new SqlParameter("@DepartPhone",departInfo.DepartPhone),
                    new SqlParameter("@DepartEmail",departInfo.DepartEmail),
                    new SqlParameter("@BranchId",departInfo.Branch.BranchId)
                };
                int result = db.getCount("proc_updateDepartInfoByDepartId", para);
                return result;
            }
            catch (Exception e)
            {

                throw e;
            }
        }
        //根据DepartId删除部门信息
        public int DeleteDepartInfoByDepartId(int departId)
        {
            try
            {
                int result = db.getCount("proc_deleteDepartInfoByDepartId", new SqlParameter("@departId", departId));
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //根据DepartName查找DepartInfo
        public DepartInfo GetDepartInfoByDepartName(string departName)
        {
            try
            {
                using (SqlDataReader reader = db.getDataReader("proc_getDepartInfoByDepartName", new SqlParameter("@departName", departName)))
                {
                    DepartInfo departInfo = new DepartInfo();
                    if (reader.Read())
                    {
                        departInfo.DepartId = (int)reader["DepartId"];
                        departInfo.DepartName = (string)reader["DepartName"];
                        departInfo.DepartPhone = (string)reader["DepartPhone"];
                        departInfo.DepartEmail = (string)reader["DepartEmail"];
                        int branchId = (int)reader["BranchId"];
                        departInfo.Branch = branchInfoService.GetBranchInfoByBranchId(branchId);
                        reader.Close();
                    }
                    db.CloseConnection();
                    return departInfo;
                }
            }
            catch (Exception e)
            {

                throw e;
            }
        }
        /// <summary>
        /// 根据branchId查找所有DepartInfo
        /// </summary>
        /// <param name="branchId">要查找的机构Id</param>
        /// <returns>要查找部门的机构信息</returns>
        public IList<DepartInfo> getAllDepartInfoByBranchId(int branchId)
        {
            List<DepartInfo> departInfoList = new List<DepartInfo>();
            try
            {
                using (DataTable table = db.getDataTable("proc_getAllDepartInfoByBranchId", new SqlParameter("@branchId", branchId)))
                {
                    foreach (DataRow row in table.Rows)
                    {
                        DepartInfo departInfo = new DepartInfo();
                        departInfo.DepartId = (int)row["DepartId"];
                        departInfo.DepartName = (string)row["DepartName"];
                        departInfo.DepartPhone = (string)row["DepartPhone"];
                        departInfo.DepartEmail = (string)row["DepartEmail"];
                        int branchID = (int)row["BranchId"];
                        departInfo.Branch = branchInfoService.GetBranchInfoByBranchId(branchID);
                        departInfoList.Add(departInfo);
                    }
                    db.CloseConnection();
                    return departInfoList;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        /// <summary>
        /// 根据branchId和departName查找DepartInfo
        /// </summary>
        /// <param name="branchId">选择的机构Id</param>
        /// <param name="departName">添加的部门名称</param>
        /// <returns>返回查找的对象</returns>
        public DepartInfo getDepartInfoByBranchIdAndDepartName(int branchId, string departName)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@branchId",branchId),
                    new SqlParameter("@departName",departName)
                };
                using (SqlDataReader reader = db.getDataReader("proc_getDepartInfoByBranchIdAndDepartName", para))
                {
                    DepartInfo departInfo = new DepartInfo();
                    if (reader.Read())
                    {
                        departInfo.DepartId = (int)reader["DepartId"];
                        departInfo.DepartName = (string)reader["DepartName"];
                        departInfo.DepartPhone = (string)reader["DepartPhone"];
                        departInfo.DepartEmail = (string)reader["DepartEmail"];
                        int branchID = (int)reader["BranchId"];
                        departInfo.Branch = branchInfoService.GetBranchInfoByBranchId(branchID);
                        reader.Close();
                    }
                    db.CloseConnection();
                    return departInfo;
                }
            }
            catch (Exception e)
            {

                throw e;
            }
        }

        /// <summary>
        /// 查询部门信息
        /// </summary>
        public IList<DepartInfo> GetDepartName(int branchId)
        {

            IList<DepartInfo> list = new List<DepartInfo>();

            using (SqlDataReader reader = db.getDataReader("sp_getDepartName", new SqlParameter("@branchId", branchId)))
            {
                while (reader.Read())
                {
                    DepartInfo dif = new DepartInfo();
                    dif.DepartName = reader["DepartName"].ToString();
                    list.Add(dif);

                }
            }
            db.CloseConnection();
            return list;

        }
    }
}
